const HOST = ' http://39.101.215.135:8089'
// const HOST = 'http://192.168.0.202:8080'
/*判断问卷状态*/
const search = location.search
let code = '' // 问卷标识
let randomCodeId = ''
let version = '' // 问卷的版本号
if(search){
    const host = decodeURI(location.search).split('=')
    if (host.length > 0) {
        code = host[host.length - 1]
        checkStatus()
    } else {
        disabledBtn()
    }
}else{
    disabledBtn()
}

function checkStatus() {
    let isok = false
    $.ajax({
        type: 'get',
        async: false,
        url: HOST + "/index/" + code,
        dataType: "text",
        success: function (res) {
            res = JSON.parse(res)
            if (res.isValid) {
                isok = true
                version = res.version
                randomCodeId = res.randomCodeId
                $('.submit .error_hint')[0].style.display = 'none'
            } else {
                disabledBtn()
            }
        },
        error: function (returndata) {
            console.log(returndata)
        }
    });
    return isok
}
function disabledBtn() {
    $('#submit')[0].disabled = true
    // $('.modal')[0].style.display = 'block'
    $('.submit .error_hint')[0].style.display = 'block'
    return false
}

function getAddress(e){
    document.getElementById('address').value = e.target.innerHTML
    document.getElementById('address_text').innerHTML = e.target.innerHTML
}

/*获取问题列表*/
$.ajax({
    type: 'get',
    async: false,
    url: HOST + "/question/tpl/list",
    dataType: "text",
    success: function (res) {
        let html = ''
        res = JSON.parse(res)
        res.data.forEach((item, index) => {
            if (item.type === 'score') {
                html +=
                    `
					<div id="que_${item.questionTplId}" value="${item.questionTplId}" logicskipfrom="logicskipfrom_str" islogicrelated="false" iswidgethidden="false" class="form-group question-score">
						<label id="${item.questionTplId}_label" class="text-semibold " maxlength="length_str">
							<span class="text-grey question-num"></span>${index + '．' + item.content}<span class="not_null">*</span><span
							 class="error_hint"><i class="icon-exclamation"></i><span>此项为必填</span></span></label>
						<div class="desc"></div>
						<div class="row">
							<div class="col-md-12" dojotype="unieap.form.Score" databind="270d0c2c-dc6f-40bf-ac69-ffda0906.${item.questionTplId}"
							 databindtext="${item.questionTplId}" variablebind="variablebind_str" variablebindtext="variablebindtext_str"
							 texttype="">
								<div class="col-xs-2" style="text-overflow: ellipsis;cursor:default;">
									<span title="非常不满意">非常不满意</span>
								</div>
								<div class="col-xs-8" style="font-size:0px;">
									<div style="font-size:8px;" class="slider-score-container">
										<div sliderminscore="0" slidermaxscore="10" class="score-slider"></div>
										<input type="text" value="0" class="sliderNowVal" hidden="" />
									</div>
								</div>
								<div class="col-xs-2" style="text-overflow: ellipsis;cursor:default;">
									<span title="非常满意">非常满意</span>
								</div>
							</div>
						</div>
					</div>
					`
            } else if (item.type === 'radio') {

                let radioHtml = ''

                item.optionList.forEach((r) => {
                    radioHtml +=
                        `
						<div class="radio" hashow="hasshow_str" logicshow="logicshow_str" haskipto="haskipto_str" logicskipto="logicskipto_str">
							<label style="word-wrap: break-word;word-break:normal;"><input value="${r}" type="radio" name="radio"
								 class="control-custom" />${r}</label>
						</div>
						`
                })

                html +=
                    `
					<div class="form-group question-radio" id="que_${item.questionTplId}" value="${item.questionTplId}" logicskipfrom="logicskipfrom_str" islogicrelated="false" iswidgethidden="false">
						<label id="${item.questionTplId}_label" class="text-semibold "><span class="text-grey question-num"></span>${index + '．' + item.content}<span
							 class="not_null">*</span><span class="error_hint"><i class="icon-exclamation"></i><span>此项为必填</span></span></label>
						<div class="desc"></div>
						<div class="row">
							<div class="col-md-12" atleast="" atmost="" dojotype="unieap.form.GroupBox" type="radio" databind="270d0c2c-dc6f-40bf-ac69-ffda0906.${item.questionTplId}"
							 databindtext="${item.questionTplId}" variablebind="variablebind_str" variablebindtext="variablebindtext_str">
								${radioHtml}
							</div>
						</div>
					</div>
					`
            }
            document.querySelector('.score').innerHTML = html
        })
    },
    error: function (returndata) {
        console.log(returndata)
    }
});
$('#back').click(function () {
    $('.panel')[0].style.display = 'block'
    $('.event_after_submit')[0].style.display = 'none'
})
/*提交数据*/
$('#submit').click(function () {
   const flg =  checkStatus()
    if(flg){
        let answerlist = []
        let param = {
            randomCodeId: randomCodeId,
            userName: '',
            phone: '',
            addr: '',
            suggest: '',
            version: version,
            remark: '',
            questionReqDtoList: []
        }


        /* 评分题 */
        const scoreList = document.querySelectorAll('.question-score')
        scoreList.forEach((item) => {
            let questionTplId = item.getAttribute('value')
            let score = document.querySelector('#que_' + questionTplId + ' .ui-slider-tip').innerHTML
            const temp = {
                questionTplId: questionTplId,
                result: score
            }
            answerlist.push(temp)
        })

        /* 评分题 */
        const radio= document.querySelectorAll('.question-radio')[0]
        let questionTplId = radio.getAttribute('value')
        const radios = document.querySelectorAll('#que_' + questionTplId + ' input[name=radio]')
        let answer = ''
        for (var i = 0; i < radios.length; i++) {
            if ($(radios[i]).context.checked) {
                answer = $(radios[i]).context.value
            }
        }
        if (answer) {
            const temp = {
                questionTplId: questionTplId,
                result: answer
            }
            document.querySelectorAll('#que_' + questionTplId + ' .error_hint')[0].style.display = 'none'
            answerlist.push(temp)
        } else {
            document.querySelectorAll('#que_' + questionTplId + ' .error_hint')[0].style.display = 'block'
            return
        }

        /*用户姓名*/
        const username = document.querySelector('#username').value
        if (username) {
            param.userName = username
            document.querySelectorAll('.username .error_hint')[0].style.display = 'none'
        } else {
            document.querySelectorAll('.username .error_hint')[0].style.display = 'block'
            return
        }

        /*用户地址*/
        const address = document.querySelector('#address').value
        if (address) {
            param.addr = address
            document.querySelectorAll('.address .error_hint')[0].style.display = 'none'
        } else {
            document.querySelectorAll('.address .error_hint')[0].style.display = 'block'
            return
        }

        /*联系电话*/
        const phone = document.querySelector('#phone').value
        if (phone) {
            let reg = new RegExp(/^1[345789]\d{9}$/);
            if (!reg.test(phone)) {
                document.querySelectorAll('.phone .error_hint')[0].innerHTML = '请填写正确的电话号码'
                document.querySelectorAll('.phone .error_hint')[0].style.display = 'block'
                return;
            }
            param.phone = phone
            document.querySelectorAll('.phone .error_hint')[0].style.display = 'none'
        } else {
            document.querySelectorAll('.phone .error_hint')[0].style.display = 'block'
            return
        }

        /*建议*/
        param.suggest = document.querySelector('#suggest').value

        param.questionReqDtoList = answerlist

        console.log(param)

        $.ajax({
            type: 'post',
            url: HOST + '/question/insert',
            contentType: 'application/json;charset=UTF-8',
            dataType: 'json',
            data: JSON.stringify(param),
            success: function (res) {
                $('.panel')[0].style.display = 'none'
                $('.event_after_submit')[0].style.display = 'block'
            },
            error: function (returndata) {
                console.log(returndata)
            }
        });

    }
})
